package com.goldsprite.infinityworld.screens.examples;

import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.goldsprite.gdxcore.screens.*;
import com.goldsprite.infinityworld.screens.examples.entity.*;
import com.goldsprite.infinityworld.screens.examples.ui.*;
import java.util.*;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.goldsprite.infinityworld.assets.GlobalAssets;
import com.goldsprite.infinityworld.screens.games.EngineEditorScreen;

public class ExampleSelectScreen extends GScreen {
	private Map<String, Class<? extends IGScreen>> screenMapping = new LinkedHashMap<String, Class<? extends IGScreen>>() {{
			put("引擎编辑器", EngineEditorScreen.class);
			put("子视口测试", ChildViewportTest.class);
			put("简单物理演示", SimplePhysicsExampleScreen.class);
			put("自定义绘制排序演示", CustomSortRendererExampleScreen.class);
			put("自定义网格地图演示[空]", GridMapExampleScreen.class);
			put("可变换布局游戏世界演示", GameWorldLayoutExampleScreen.class);
			put("WidgetGroup变换演示", WidgetGroupTransformExampleScreen.class);
			put("日志查看器", LogViewerExampleScreen.class);
			put("FBO材质缓冲输出绘制演示", FBOExampleScreen.class);
			put("游戏世界演示: 动画机实体", AnimatorExampleScreen.class);
			put("Touchpad摇杆演示", TouchpadExampleScreen.class);
			put("大型tilemap性能演示以及WindowConsole", TiledMapBench.class);
			put("Window窗口使用演示", WindowExampleScreen.class);
			put("SplitPane布局分栏使用演示", SplitPaneExampleScreen.class);
			put("视口坐标理解演示", ViewportCoordExampleScreen.class);
			put("Gdx视口演示", ViewportTest1.class);
		}};
	private Skin skin;
	private Stage stage;
	private float layoutInScreenScl = 4 / 5f;
	private float layout_padding = 80;
	private float cell_margin = 30;
	private float cell_width = -1;
	private float cell_height = 50;
	private Table rootTable;


	@Override
	public void create() {
		skin = GlobalAssets.getInstance().defaultSkin;

		stage = new Stage(getViewport());
		getImp().addProcessor(stage);

		float stageWidth = stage.getWidth();
		float stageHeight = stage.getHeight();

		//创建主视图
		rootTable = new Table();
		float fix = layoutInScreenScl;
		rootTable.setSize(stageWidth * fix, stageHeight * fix);

		// 将 rootTable 居中
		Vector2 rootTablePos = new Vector2();
		rootTablePos.set(stageWidth / 2, stageHeight / 2);
		rootTablePos.sub(rootTable.getWidth() / 2, rootTable.getHeight() / 2);
		rootTable.setPosition(rootTablePos.x, rootTablePos.y);
		stage.addActor(rootTable);

		// 按钮列表
		Table buttonList = getButtonTable();

		// 将按钮列表包裹在滚动面板中
		ScrollPane scrollPane = new ScrollPane(buttonList, skin);
		scrollPane.setScrollingDisabled(true, false); // 禁用水平滚动，启用垂直滚动
		scrollPane.setFadeScrollBars(true); // 保持滚动条可见
		rootTable.add(scrollPane).expand().fill();
	}

	//返回vertical按钮列表Table
	private Table getButtonTable() {
		Table buttonTable = new Table();
		buttonTable.pad(layout_padding)//内边距
			.defaults().space(cell_margin); //默认单元格间距

		//创建按钮列表
		for (String title : screenMapping.keySet()) {
			Class<? extends IGScreen> key = screenMapping.get(title);
			TextButton button = new TextButton(title, skin);
			Cell<TextButton> cell = buttonTable.add(button);

			if (cell_width > -1) cell.width(cell_width);
			else cell.expandX().fillX();
			cell.height(cell_height);

			buttonTable.row();
			button.addListener(new ClickListener() {
					@Override
					public void clicked(InputEvent event, float x, float y) {
						getScreenManager().setCurScreen(key, true);
					}
				});
		}
		return buttonTable;
	}

	@Override
	public void render(float delta) {
		super.render(delta);

		stage.act(delta);
		stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
		rootTable.setSize(stage.getWidth() * layoutInScreenScl, stage.getHeight() * layoutInScreenScl);
	}

	@Override
	public void dispose() {
		stage.dispose();
		skin.dispose();
	}

}
